package com.gcit.seniorWish.web.modular.inputMidScore.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.gcit.seniorWish.core.cache.CacheKit;
import com.gcit.seniorWish.web.common.constant.cache.Cache;
import com.gcit.seniorWish.web.common.constant.cache.CacheKey;
import com.gcit.seniorWish.web.common.persistence.dao.SeniorSchoolClassMapper;
import com.gcit.seniorWish.web.common.persistence.model.SeniorSchoolClass;
import com.gcit.seniorWish.web.core.shiro.ShiroUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gcit.seniorWish.core.base.controller.BaseController;
import com.gcit.seniorWish.core.base.tips.ErrorTip;
import com.gcit.seniorWish.core.base.tips.Tip;
import com.gcit.seniorWish.core.exception.GunsException;
import com.gcit.seniorWish.web.common.constant.factory.PageFactory;
import com.gcit.seniorWish.web.common.persistence.dao.ExamineeScoreMapper;
import com.gcit.seniorWish.web.common.persistence.model.ExamineeScore;
import com.gcit.seniorWish.web.core.exception.BizExceptionEnum;
import com.gcit.seniorWish.web.modular.inputMidScore.service.IExamineeScoreService;
import com.gcit.seniorWish.web.modular.inputMidScore.warpper.ExamineeScoreWarpper;
import com.gcit.seniorWish.web.modular.system.service.IUserMgrService;

import cn.hutool.core.util.StrUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 中考成绩导入控制器
 *
 * @author fengshuonan
 * @Date 2018-03-23 14:10:03
 */
@Controller
@RequestMapping("/inputMidScore")
public class ExamineeScoreController extends BaseController {

	private String PREFIX = "/inputMidScore/examineeScore";

	@Autowired
	private IExamineeScoreService examineeScoreService;

	@Resource
	private ExamineeScoreMapper examineeScoreMapper;

	@Autowired
	private IUserMgrService userMgrService;

	/**
	 * 跳转到中考成绩导入首页
	 */
	@RequestMapping("")
	public String index() {

		return PREFIX + "/examineeScore.html";
	}

	/**
	 * 跳转到添加中考成绩导入
	 */
	@RequestMapping("/examineeScore_add")
	public String examineeScoreAdd() {

		return PREFIX + "/examineeScoreAdd.html";
	}

	/**
	 * 新增中考成绩导入
	 */

	@RequestMapping(value = "/add")
	@ResponseBody
	public Object add(ExamineeScore examineeScore) {
		return PREFIX + "/examineeEdit.html";
	}

	/**
	 * 中考成绩新增
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/save")
	@ResponseBody
	public Tip save(ExamineeScore examineeScore) {

		System.out.println("准考证号为：" + examineeScore.getExamCardNumber());

		ExamineeScore exam = examineeScoreMapper.selectOne(examineeScore);
		if (exam != null) {// 数据库已存在
			if (!exam.getExamCardNumber().equals(examineeScore.getExamCardNumber())) {
				if (this.examineeScoreService.selectCount(Condition.create().eq("exam_card_number", examineeScore.getExamCardNumber())) > 0) {// 该准考证号已存在
					throw new GunsException(BizExceptionEnum.EXAM_CARD_NUMBER_EXIST);
				}
			}
		} else {// 新增数据
			if (this.examineeScoreService.selectCount(Condition.create().eq("exam_card_number", examineeScore.getExamCardNumber())) > 0) {// 该准考证号已存在
				throw new GunsException(BizExceptionEnum.EXAM_CARD_NUMBER_EXIST);
			}
		}

		examineeScoreService.insert(examineeScore);

		return SUCCESS_TIP;

	}

	/**
	 * 中考成绩更改页面跳转
	 */

	@RequestMapping(value = "/edit")
	public Object edit(String id, Model model) {

		ExamineeScore examineeScore = examineeScoreService.selectById(id);
		model.addAttribute("examineeScore", examineeScore);
		return PREFIX + "/examineeScore_edit.html";
	}

	/**
	 * 中考成绩成绩更新
	 *
	 * @param examineeScore
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Object update(ExamineeScore examineeScore) {
		System.out.println(examineeScore.toString());
		examineeScoreService.updateById(examineeScore);
		return SUCCESS_TIP;
	}


	/**
	 * 获取中考成绩导入列表
	 * /**
	 * 获取考生列表
	 */
	@SuppressWarnings({"unchecked", "rawtypes"})
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(@RequestParam(required = false) String examCardNumber, @RequestParam(required = false) String studentName,
	                   @RequestParam(required = false) String senoirSchoolName, HttpServletRequest request) {

		ShiroUser shiroUser = (ShiroUser) request.getSession().getAttribute("shiroUser");

		Page<Map<String, Object>> page = new PageFactory().defaultPage();

		EntityWrapper<ExamineeScore> ew = new EntityWrapper<ExamineeScore>();

		if (StrUtil.isNotBlank(shiroUser.getClassId())) {
			ew.eq("senoir_class_id", shiroUser.getClassId());
		}

		if (StrUtil.isNotBlank(examCardNumber)) {
			ew.like("exam_card_number", examCardNumber);
		}

		if (StrUtil.isNotBlank(studentName)) {
			ew.like("student_name", studentName);
		}

		if (StrUtil.isNotBlank(senoirSchoolName)) {
			ew.like("senoir_school_name", senoirSchoolName);
		}

		examineeScoreService.selectMapsPage(page, ew);

		page.setRecords((List<Map<String, Object>>) new ExamineeScoreWarpper(page.getRecords()).warp());

		return packForBT(page);

	}


	/**
	 * 删除中考成绩
	 */

	@RequestMapping(value = "/remove")
	@ResponseBody
	public Object delete(@RequestParam String examineeScoreId,String examCardNumber) {
		examineeScoreService.deleteById(examineeScoreId);
		//手动删除查询成绩时，缓存的数据
		CacheKit.remove(Cache.BUSS,CacheKey.EXAMINEE_SCORE_BY_EXAM_CARD+examCardNumber);
		return SUCCESS_TIP;
	}

	/**
	 * 根据模板导入中考成绩信息
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/import")
	@ResponseBody
	public Object ImportMidScore(@RequestPart("file") MultipartFile excelFile, HttpServletRequest request) {
		ShiroUser user = (ShiroUser) request.getSession().getAttribute("shiroUser");
		try {
			String msg = examineeScoreService.importMidScore(excelFile.getInputStream(), user);
			if (StrUtil.isBlank(msg)) {
				msg = "导入成功";
			}
			//返回导入结果
			return new Tip(200, msg);
		} catch (IOException e) {
			e.printStackTrace();
			return new ErrorTip(BizExceptionEnum.SERVER_ERROR.getCode(), BizExceptionEnum.SERVER_ERROR.getMessage());
		}
	}

}
